<?php



class ChatsController extends AppController{
    
    public $uses = array('ChatsMessage');
    
    
    static $placeId = 5;
    
    public function view($placesId){
        
        $data = $this->ChatsMessage->find('all', array(            
            'conditions' => array(
                'ChatsMessage.places_id' => $placesId,
            ),
            'order' => array(
                'ChatsMessage.posted_on DESC' 
            ),
            'limit' => 10
        ));
        
        $this->set('placeId', $placesId);
        $this->set('data', $data);
    }
    
    public function getLastMessages(){   
        
        $lastUpdate = $this->request->data['lastUpdate'];
        
        if (empty($lastUpdate))
            $lastUpdate = 1;
            
        $placeId = $this->request->data['places_id'];
        
        $data = $this->ChatsMessage->find('all', array(
            'conditions' => array(
                'ChatsMessage.places_id' => $placeId,
                'ChatsMessage.id > ' => $lastUpdate
            ),
            'order' => array(
                'ChatsMessage.posted_on DESC' 
            )
        ));
        
        /*
        if (!empty($data[0])){
            self::$dateLastMessage = $data[0]['ChatsMessage']['posted_on'];
        }*/
        
        $this->set('data', $data);
        
    }
    
    public function add(){
        
        
        $data = $this->request->data;
        
        $data['users_id'] = $this->getCurrentUserId();
        $data['posted_on'] = date('c');
        
        
        $this->ChatsMessage->save($data);
        
    }
    
    
}
?>
